import { getShopSellerReq, getGoodsListReq, getShopRatingsReq } from '@/api/goods'
const state = {
    shopData: {},
    goodsList: [], //商品列表
    commentList: [], //评价列表
}
const getters = {
    navList(state) {
        return state.goodsList.map(v => v.name)
    },
    //评论筛选
    goodRateList(state) {
        return state.commentList.filter(v => {
            return v.score > 3
        })
    },
    badRateList(state) {
        return state.commentList.filter(v => {
            return v.score <= 3
        })
    },
}
const mutations = {
    SET_SHOPINFO(state, data) {
        state.shopData = data
    },
    SET_GOODS_LIST(state, data) {
        state.goodsList = data
    },
    SET_RATING(state, data) {
        state.commentList = data
    },
}
const actions = {
    //获取店铺数据
    A_getShopInfo({ commit }) {
        return new Promise(async resolve => {
            let res = await getShopSellerReq()
            let { data } = res.data
                // console.log('res :>> ', res)
                //数据赋值
            commit('SET_SHOPINFO', data)
                // console.log('this.shopData :>> ', this.shopData)
            resolve()
        })
    },
    A_getGoodsList({ commit }) {
        return new Promise(async resolve => {
            let res = await getGoodsListReq()
            console.log('res :>> ', res);
            let { code, data } = res.data
            if (code === 0) {
                //数据赋值
                commit('SET_GOODS_LIST', data)
                resolve()
            }
        })
    },
    //获取评价
    A_getCommentData({ commit }) {
        return new Promise(async resolve => {
            let res = await getShopRatingsReq()
            console.log("res :>> ", res.data.data.avatar);
            let { errno, data } = res.data
            if (errno === 0) {
                commit('SET_RATING', data)
                resolve()
            }
        })
    },
}
export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions,
}